package cn.hjh.aop;

import cn.hjh.entity.Equip;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * 使用注解定义切面
 */
@Aspect
public class TheLogger {

    private static Logger log = Logger.getLogger(TheLogger.class);

    //前置增强方法
    //@Before("execution(* cn.hjh.service.UserService.*(..))")
    @Pointcut("execution(* cn.hjh.service.UserService.*(..))")
    public void pointcut(){}

    @Before("pointcut()")
    public void before(JoinPoint jp){
        log.info("调用" + jp.getTarget() + "的" + jp.getSignature().getName() + "方法。方法入参:" + Arrays.toString(jp.getArgs()));
    }

    //后置增强方法
    //@AfterReturning(pointcut = "execution(* cn.hjh.service.UserService.*(..))",returning = "result")
    @AfterReturning(pointcut = "pointcut()",returning = "result")
    public void after(JoinPoint jp,Object result){
        log.info("调用" + jp.getTarget() + "的" + jp.getSignature().getName() + "方法。方法返回值:" + result);
    }


    @Pointcut("execution(public static void print(cn.hjh.entity.Equip))")
    @Before("pointcut()")

    public void jdt5befor(JoinPoint jp){

        Equip equip = (Equip) jp.getArgs()[0];

        if (equip!=null&&equip.getType().equals("指环")){
            equip.setName("紫色梦幻");
            equip.setAttackPlus(equip.getAttackPlus()+6);
            equip.setDefencePlus(equip.getDefencePlus()+6);
        }else {
            System.out.println("不是指环类");
        }
    }

}
